﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace HY.BLL.oa
{
  public  class leave_sub
    {

      public static string getstatus(string bh)
      {
          string val = "未审批";
          string sqltxt = "select * from [leave_sub] where [reviewBH]='"+bh+"'";
          DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
          string nval = "";
          int j = 0,s=0;

          for (int i=0;i<dt.Rows.Count;i++)
          {
              if (!String.IsNullOrEmpty(dt.Rows[i]["reviewrun"].ToString()))
              {
                  j = j + 1;
              }
              else
              {
                 if(dt.Rows.Count==1) j = -1;
              }

              if (dt.Rows[i]["reviewrun"].ToString() == "不同意")
              {
                  s = s + 1;
              }

          }

          if (j == dt.Rows.Count)
              val = "审批完成";

          if (j > 0)
          {
              val = "审批中...";
              if (j == dt.Rows.Count)
                  val = "审批完成";

          }
          if (s > 0)
              val = "未通过审批";

          if (val != "未审批")
              DAL.DbHelperSQL.ExecuteNonQuery("update [review_leave] set [status]=N'"+val+"' where bh='"+bh+"'");
         
          return val;
      }


      public static Boolean  check_approl(string bh)
      {
          string sqltxt = "select * from [leave_sub] where [reviewrun] is not null and [reviewBH]='" + bh + "'";
          DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
         
          sqltxt = "select * from [leave_sub] where [reviewrun] is null and [reviewBH]='" + bh + "'";
          DataTable dt2 = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);



          if (dt.Rows.Count > dt2.Rows.Count)
              return true;  //已完批完成
          else
              return false; //未审批完成

      }

      public static DataTable getlist(string bh)
      {
          string sqltxt = "select * from [leave_sub] where [reviewBH]='" + bh + "'";
          DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
          return dt;
      }


      //未审批
      public static DataTable getleaveapp_n(string nm,string whe)
      {

          string sqltxt = "select b.* from (select reviewBH,reviewnm,reviewrun from [leave_sub] where reviewnm='" + nm + "' and reviewrun is null  group by reviewBH,reviewnm,reviewrun) as a," +
              "review_leave as b where a.reviewBH=b.BH ";

          if (whe != "")
              sqltxt = sqltxt + whe;

          DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
          return dt;
      }

      //已审批
      public static DataTable getleaveapp_y(string nm,string whe)
      {

          string sqltxt = "select b.* from (select reviewBH,reviewnm,reviewrun from [leave_sub] where reviewnm='" + nm + "' and  reviewrun is not null  group by reviewBH,reviewnm,reviewrun) as a," +
              "review_leave as b where a.reviewBH=b.BH ";//and a.reviewnm='" + nm + "' ";

          if (whe != "")
              sqltxt = sqltxt + whe;

          DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
          return dt;
      }

      //全部
      public static DataTable getleaveapp_all(string nm,string whe)
      {

          string sqltxt = "select b.* from (select reviewBH,reviewnm,reviewrun from [leave_sub] where  reviewnm='" + nm + "'   group by reviewBH,reviewnm,reviewrun) as a," +
              "review_leave as b where a.reviewBH=b.BH ";//and a.reviewnm='" + nm + "' ";

          if (whe != "")
              sqltxt = sqltxt + whe;

          DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
          return dt;
      }

      /// <summary>
      /// 添加审批记录
      /// </summary>
      /// <param name="bh">审批单号</param>
      /// <param name="nm">审批人</param>
      /// <param name="val">审批结果</param>
      /// <param name="memo">批注</param>
      /// <returns></returns>
      public static int ins_data(string bh, string nm, string val,string memo)
      {
          string sqltxt = "insert into leave_sub values(N'" + bh + "',N'" + nm + "',N'" + val + "',N'" + memo + "')";
          return DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
      }


      public static int up_data(string bh, string nm, string val, string memo)
      {
          string sqltxt = "update  leave_sub  set [reviewrun]=N'" + val + "',[reviewmem]=N'" + memo + "' where [reviewBH]='" + bh + "' and [reviewnm]='" + nm + "'";
          return DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
      }


      //public static int exist_data(string bh, string nm)
      //{
      //    string sqltxt = "insert into leave_sub('" + bh + "','" + nm + "','" + val + "','" + memo + "')";
      //    return DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
      //}


      /// <summary>
      /// 获取匹配表
      /// </summary>
      /// <param name="whe"></param>
      /// <returns></returns>
      public static DataTable getlist_eqau(string whe)
      {
          string sqltxt = "select * from [oa_form_eqau] where 1=1  ";
          
          if (whe != "") sqltxt = sqltxt + " and " + whe;
          sqltxt = sqltxt + " order by sid desc ";

          DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
          return dt;
      }


      /// <summary>
      /// 获取匹配单名称
      /// </summary>
      /// <param name="whe"></param>
      /// <returns></returns>
      public static string get_eqau_val(string whe)
      {
          string sqltxt = "select [forproces] from [oa_form_eqau] where 1=1  ";
          string val = "";
          if (whe != "") sqltxt = sqltxt + " and " + whe;
          sqltxt = sqltxt + " order by sid desc ";

          DataTable dt = DAL.DbHelperSQL.SqlDataAdapter(sqltxt);
          if (dt.Rows.Count > 0)
              val = dt.Rows[0]["forproces"].ToString();
          else
              val = "";


          return val;
      }



      /// <summary>
      /// 更新匹配表
      /// </summary>
      /// <param name="whe"></param>
      /// <returns></returns>
      public static int uplist_eqau(string sid,string proc)
      {
          string sqltxt = "update [oa_form_eqau] set forproces=N'" + proc + "' where formname='" + sid + "'  ";

       return  DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);          
      }

      /// <summary>
      /// 删除匹配表
      /// </summary>
      /// <param name="whe"></param>
      /// <returns></returns>
      public static int dellist_eqau(string sidlist)
      {
          string sqltxt = "update [oa_form_eqau] set forproces=''  where sid in(" + sidlist + ")  ";

          return DAL.DbHelperSQL.ExecuteNonQuery(sqltxt);
      }


    }
}
